﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eVoteClient.util;

namespace eVoteClient
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (studyGroups.Text == "")
            {
                MessageBox.Show("Please choose a study group!!");
                return;
            }

            global.socket = new Socket_io(global.destIP, global.destPort);
            var socket = global.socket;

            string enc = Encryption.sh1(passwordTxt.Text);
            socket.writer.WriteLine("REG_REQ#" + emailTxt.Text + "#" + enc + "#" + studyGroups.Text);
            socket.writer.Flush();

            string reply = socket.reader.ReadLine();
            if (reply == "REG_RES#1")
            {
                MessageBox.Show("Registeration complete. You will receive an Activation code on your email now. Please activate your account using that code");
                button2.Visible = true;
                panel1.Enabled = false;
                button1.Enabled = false;
            }
            else if (reply == "REG_RES#0")
            {
                MessageBox.Show("This Email already exists. You maybe already registered in the system or you entered a wrong email.");
            }
        }


        private void passwordTxt_TextChanged(object sender, EventArgs e)
        {
            refreshButton();
        }

        private void confirmPasswordTxt_TextChanged(object sender, EventArgs e)
        {
            refreshButton();
        }


        private void refreshButton()
        {
            if (passwordTxt.Text != confirmPasswordTxt.Text || passwordTxt.Text.Trim()=="")
            {
                button1.Enabled = false;
                errorLbl.Text = "Password is required and Confirmation should match the password";
            }
            else if ( ! email(emailTxt.Text))
            {
                
                button1.Enabled = false;
                errorLbl.Text = "Email not valid";
            }
            
            else
            {
                button1.Enabled = true;
                errorLbl.Text = "";
            }
        }



        private bool email(string e)
        {
            if (e.Trim() == "")
                return false;
            if (e.LastIndexOf("@") != e.IndexOf("@"))
                return false;
            if (e.Contains(".."))
                return false;
            if (e.IndexOf("@") == 0)
                return false;
            if (e.LastIndexOf(".") < e.IndexOf("@"))
                return false;
            return true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new ActivateAccount().Show();
            this.Hide();
        }

        private void label4_Click(object sender, EventArgs e)
        {

            Application.ExitThread();
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
            t.Start();
        }

        public static void ThreadProc()
        {
            Application.Run(new Login());
        }

        private void Register_Load(object sender, EventArgs e)
        {
            this.Location = new Point(350, 200);
            this.Size = new Size(572, 324);
            
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }


    }
}
